package cn.cloud.butler.webapi.controller;

import cn.cloud.butler.webapi.domain.MacGroupEntity;
import cn.cloud.butler.webapi.domain.RenameEntity;
import cn.cloud.butler.webapi.domain.ResponseEntity;
import cn.cloud.butler.webapi.domain.ResponseEntityNoData;
import cn.cloud.butler.webapi.logger.WaLogger;
import cn.cloud.butler.webapi.service.MachineGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * author: daichaoren
 * time: 2019-01-09
 * description: 族操作类接口
 */

@RestController
@EnableAutoConfiguration
@RequestMapping("/HncWeb")
public class MachineGroupController {
    private final MachineGroupService machineGroupService;
    @Autowired
    public MachineGroupController(MachineGroupService machineGroupService) {
        this.machineGroupService = machineGroupService;
    }


    /**
     * 机床用户厂组重命名
     * 10.10.56.19:8080/HncWeb/Rename.action?Username=zgc3&Name=zgcName&Rename=2222
     * @param renameEntity 重命名实体类
     * @return ResponseEntityNoData
     */
    @RequestMapping(method = RequestMethod.POST, value = "/Rename.action")
    public ResponseEntityNoData rename(RenameEntity renameEntity){
        ResponseEntityNoData response = new ResponseEntityNoData();
        try {
            Map<String, Object> map = machineGroupService.RenameByUsername(renameEntity);
            response.setMsgCode(Integer.parseInt(map.get("MsgCode").toString()));
            response.setMsgDesc(map.get("MsgDesc").toString());
        }catch (Exception e){
            response.setMsgCode(1);
            response.setMsgDesc(e.toString());
        }
        return  response;
    }


    /**
     * 机床用户厂组重命名
     * 10.10.56.19:8080/HncWeb/Rename.action?Username=zgc3&Name=zgcName&Rename=2222
     * @param groupId 重命名实体类
     * @return ResponseEntityNoData
     */
    @RequestMapping(method = RequestMethod.GET, value = "/selectGroup.action")
    public ResponseEntity selectGroup(Long groupId){
        ResponseEntity response = new ResponseEntity();
        try {
            response.setMsgCode(0);
            response.setMsgDesc("查询成功");
            if(machineGroupService.FindGroupByGroupId(groupId) == null){
                MacGroupEntity macGroupEntity = new MacGroupEntity();
                macGroupEntity.setName("生产车间");
                macGroupEntity.setiD(-1l);
                response.setData(macGroupEntity);
            }else {
                response.setData(machineGroupService.FindGroupByGroupId(groupId));
            }
        }catch (Exception e){
            response.setMsgCode(1);
            response.setMsgDesc(e.toString());
        }
        return  response;
    }


    /**
     * zgc
     * 添加机床用户厂组
     * http://10.10.56.19:8084/HncWeb/AddGroup.action?Username=13163295907&Name=6664
     * @param Username 用户名
     * @param Name 组名
     * @return ResponseEntityNoData
     */
    @RequestMapping(method = RequestMethod.POST,value = "/AddGroup.action")
    public ResponseEntityNoData addMacGroup(String Username,String Name){
        ResponseEntityNoData responseEntityNoData = new ResponseEntityNoData();
        try {
            WaLogger.logger.debug("进入函数--------");
            Map<String, Object> map = machineGroupService.addMacGroup(Username, Name);
            responseEntityNoData.setMsgCode(Integer.parseInt(map.get("MsgCode").toString()));
            responseEntityNoData.setMsgDesc(map.get("MsgDesc").toString());
        }catch (Exception e){
            WaLogger.logger.warn(e.toString());
            responseEntityNoData.setMsgCode(1);
            responseEntityNoData.setMsgDesc(e.toString());
        }
        return   responseEntityNoData;
    }


    /**
     * zgc
     * 删除机床用户组
     * 10.10.56.19:8080/HncWeb/DeleteMacSN.action?Username=13163295907&Name=666&MacSN=003
     * @param Username 用户名
     * @param Name 组名
     * @param MacSN 机床sn
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value ="/DeleteMacSN.action")
    public  ResponseEntityNoData deleteMacSN(String Username,String Name,String MacSN ){
        ResponseEntityNoData responseEntityNoData = new ResponseEntityNoData();
        try{
            HashMap<String,Object> map = machineGroupService.deleteMacSN(Username,Name,MacSN);
            responseEntityNoData.setMsgCode(Integer.parseInt(map.get("MsgCode").toString()));
            responseEntityNoData.setMsgDesc(map.get("MsgDesc").toString());
        }catch (Exception e){
            WaLogger.logger.warn(e.toString());
            responseEntityNoData.setMsgCode(1);
            responseEntityNoData.setMsgDesc(e.toString());
        }
        return   responseEntityNoData;
    }


    /**
     * zgc
     * 机床分组里面批量添加macsn
     * 10.10.56.19:8080/HncWeb/AddMacSNs.action?Username=13163295907&Name=666&MacSN=666,004
     * @param Username 用户名
     * @param Name 组名
     * @param MacSN 机床sn列表
     * @return ResponseEntityNoData
     */
    @RequestMapping(method = RequestMethod.POST,value = "/AddMacSNs.action")
     public ResponseEntityNoData BatchAddMacSN(String Username,String Name,String MacSN){
        ResponseEntityNoData responseEntityNoData = new ResponseEntityNoData();
        try{
            HashMap<String,Object> map = machineGroupService.BatcheAddMacSN(Username, Name,MacSN);
            responseEntityNoData.setMsgCode(Integer.parseInt(map.get("MsgCode").toString()));
            responseEntityNoData.setMsgDesc(map.get("MsgDesc").toString());
        }catch (Exception e){
            responseEntityNoData.setMsgCode(1);
            responseEntityNoData.setMsgDesc(e.toString());
        }
        return responseEntityNoData;
    }

    /**
     * zgc
     * 机床分组里删除组
     * http://10.10.56.19:8080/HncWeb/DeleteGroup.action?Username=13163295907&Name=666
     * @param Username 用户名
     * @param Name 组名
     * @return ResponseEntityNoData
     */
    @RequestMapping(method = RequestMethod.POST,value = "/DeleteGroup.action")
    public ResponseEntityNoData  DeleteGroup(String Username ,String Name){
        ResponseEntityNoData responseEntityNoData = new ResponseEntityNoData();
        try {
            HashMap<String, Object> map = machineGroupService.DeleteGroup(Username, Name);
            responseEntityNoData.setMsgCode(Integer.parseInt(map.get("MsgCode").toString()));
            responseEntityNoData.setMsgDesc(map.get("MsgDesc").toString());
        }catch (Exception e){
            responseEntityNoData.setMsgCode(1);
            responseEntityNoData.setMsgDesc(e.toString());
            WaLogger.logger.warn(e.toString());
        }
        return   responseEntityNoData;
    }
}
